package common;

import gui.GuiManager;

import java.awt.event.ActionEvent;
import java.util.Properties;

import javax.swing.AbstractAction;

import common.utils.CommonUtils;

public class Setup {

	private static final String DB_PROPERTIES = "config/db.properties";
	
	public static void main(String[] args) {
		Properties properties = null;
		try {
			properties = CommonUtils.readPropertiesFile(DB_PROPERTIES);
		} catch (Exception e) {}
		if (properties == null) {
			properties = new Properties();
		}
		AbstractAction action = new ApplySetupAction(properties);
		GuiManager.showSetupFrame(properties, action);
	}
	
	public static class ApplySetupAction extends AbstractAction {

		private static final long serialVersionUID = 1L;
		private final Properties properties;

		public ApplySetupAction(Properties properties) {
			this.properties = properties;	
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String comments = "Database configuration file:";
			try {
				CommonUtils.writePropertiesFile(properties, DB_PROPERTIES, comments);
			} catch (Exception e) {
				System.err.println("Setup faild!!!");
				System.err.println("The following exception occurred: " + e.getLocalizedMessage());
			}		
		}
	}
	
}
